package com.assets.util;

import android.content.Context;
import android.media.MediaPlayer;

import com.assets.R;

import java.io.IOException;

public class PlaySoundUtil {


    private static MediaPlayer mediaPlayer;

    public static MediaPlayer getMediaPlayer(Context context) {

        if (mediaPlayer == null) {
            synchronized (PlaySoundUtil.class) {
                mediaPlayer = MediaPlayer.create(context, R.raw.msg);
            }
        }


//

       return mediaPlayer;

   }

    public void start() {
        if (mediaPlayer != null) {
            mediaPlayer.seekTo(0); // 将音频播放进度重置到起始位置
            mediaPlayer.start(); // 启动播放
        }
    }

    public void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause(); // 暂停播放而不是直接停止
            mediaPlayer.seekTo(0); // 重置播放进度到起始位置
        }
    }



}
